# /usr/bin/python3

# Author: 爱编程的章老师
# @Time: 2021/1/6 0006
# E-mail: Bluesand2010@163.com


import sys
import pygame
import time

def homework():

    '''第七课作业:自落体'''
    '''
    1. 画一个棕色的扁长方形当地板
    2. 画一个蓝色小正方形(有能力的可以画球)当小球
    3. 小球自上而下.起始高度差200
    4. 每次落地后反弹高度为掉落高度的一半.直到反弹高度<5时停止
    5. 不考虑加速度.即速度在下落与反弹过程中假高是恒定的
    '''

    pygame.init()
    s = pygame.display.set_mode((800, 600))

    # 地板: floor
    floor = pygame.Rect(0, 550, 800, 50)
    floor_color  = 139, 90, 43,
    pygame.draw.rect(s, floor_color, floor)

    # 球: ball
    ball = pygame.Rect(390, 330,20,20)
    ball_color = 0, 0, 255
    pygame.draw.circle(s, ball_color, ball.center, 10)

    # 球的下面距离地板上面的距离: distance
    distance = (floor.top - ball.bottom)//2

    # 运动标记:是向上,还是向下
    dy = 1 # 表示向下, -1 表示向上
    print(distance)
    pygame.display.update()
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        ball.move_ip(0, dy)
        if dy == 1:
            if ball.colliderect(floor):
                dy = -1
        elif floor.top - ball.bottom  >= distance:
            dy = 1
            distance //=2
        if distance < 5:
            break
        s.fill((0, 0,0 ))
        pygame.draw.circle(s, ball_color, ball.center, 10)
        pygame.draw.rect(s, floor_color, floor)
        pygame.display.update()
        time.sleep(0.01)


def event_quit():
    # 退出事件不退出哦
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("我就是不退出")

def event_activeevent():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.ACTIVEEVENT:
                if event.gain == 1:
                    print("窗口是激活的")
                elif event.gain == 0:
                    print("窗口不是激活的")

def event_keydown():
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    rect = pygame.Rect(400, 300, 100,100)
    pygame.draw.rect(s, (0,0, 255), rect)

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    rect.move_ip(0, -1)
                elif event.key == pygame.K_DOWN:
                    rect.move_ip(0,1)
                elif event.key == pygame.K_LEFT:
                    rect.move_ip(-1, 0)
                elif event.key == pygame.K_RIGHT:
                    rect.move_ip(1, 0)
            s.fill((0,0,0))
            pygame.draw.rect(s, (0,0,255), rect)
            pygame.display.update()

def event_hold_keypressed():
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    rect = pygame.Rect(400, 300, 100, 100)
    pygame.draw.rect(s, (0, 0, 255), rect)

    # 新增的代码
    pygame.key.set_repeat(pygame.KEYDOWN, 10)
    # 新增结束

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_UP:
                    rect.move_ip(0, -1)
                elif event.key == pygame.K_DOWN:
                    rect.move_ip(0,1)
                elif event.key == pygame.K_LEFT:
                    rect.move_ip(-1, 0)
                elif event.key == pygame.K_RIGHT:
                    rect.move_ip(1, 0)

            s.fill((0, 0, 0))
            pygame.draw.rect(s, (0, 0, 255), rect)
            pygame.display.update()

def event_keydown2():
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    rect = pygame.Rect(400, 300, 100, 100)
    pygame.draw.rect(s, (0, 0, 255), rect)

    # 新增的代码
    pygame.key.set_repeat(pygame.KEYDOWN, 10)
    # 新增结束

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            # 改动的代码
            keys = pygame.key.get_pressed()
            print(type(keys))
            if keys[pygame.K_UP]:
                rect.move_ip(0, -1)
            elif keys[pygame.K_DOWN]:
                rect.move_ip(0, 1)
            elif keys[pygame.K_LEFT]:
                rect.move_ip(-1, 0)
            elif keys[pygame.K_RIGHT]:
                rect.move_ip(1, 0)
            # 改动结束

            s.fill((0, 0, 0))
            pygame.draw.rect(s, (0, 0, 255), rect)
            pygame.display.update()



if __name__ == '__main__':
    event_keydown2()